package com.weixing.mall.provider.controller.admin;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.weixing.mall.base.enums.SnCodeEnum;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.NoRepeatSubmit;
import com.weixing.mall.base.enums.WhetherEnum;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.core.support.auth.SysAuth;
import com.weixing.mall.core.support.auth.SysInfo;
import com.weixing.mall.provider.api.service.ItemFeign;
import com.weixing.mall.provider.model.domain.InDepotBill;
import com.weixing.mall.provider.model.domain.InDepotBillProduct;
import com.weixing.mall.provider.model.dto.InDepotBillDto;
import com.weixing.mall.provider.model.query.DepotBillDetilQuery;
import com.weixing.mall.provider.model.query.InDepotBillQuery;
import com.weixing.mall.provider.model.vo.InDepotBillVo;
import com.weixing.mall.provider.service.IInDepotBillProductService;
import com.weixing.mall.provider.service.IInDepotBillService;
import com.weixing.mall.provider.support.excaldata.ProductStockED;
import com.weixing.mall.provider.support.excalservice.DepotExcalService;
import com.weixing.mall.provider.support.excalservice.InDepotBillDataListener;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @ClassName InDepotBillController
 * @Description TODO <br/>
 * @Author gjq
 * @Modified By:
 * @Date 2019/10/22
 * @Version 1.0
 **/
@RequestMapping("inDepotBill")
@RestController
@Api(value = "入库单Api",tags = "入库单")
public class InDepotBillController extends BaseController {

    @Autowired
    private IInDepotBillService inDepotBillService;
    @Autowired
    private IInDepotBillProductService inDepotBillProductService;
    @Autowired
    private ItemFeign itemFeign;

    @ApiOperation(value = "查询入库单列表")
    @GetMapping("")
    public Result selectOfPage(InDepotBillQuery inDepotBillQuery){
        if(ObjectUtil.isNull(inDepotBillQuery.getType())){
            inDepotBillQuery.setType(1);
        }
        return ResultUtil.success(inDepotBillService.selectOfPage(initMpPage(),inDepotBillQuery));
    }
    @ApiOperation(value = "新增初始化入库单")
    @GetMapping("/initAdd")
    public Result initAdd(){
         return ResultUtil.success(inDepotBillService.getInBillSn(SnCodeEnum.INBILLSN.getValue()));

    }
    @ApiOperation(value = "根据id查找入库单")
    @GetMapping("getInDepotBillById/{id}")
    public Result getInDepotBillById(@PathVariable Long id){
        return ResultUtil.success(inDepotBillService.getInDepotBillById(id));
    }

    @ApiOperation(value = "库存管理-入库单详情导出Excal")
    @GetMapping("excel/{id}")
    public void excel(@PathVariable Long id, HttpServletResponse response){
        InDepotBillVo idbv = inDepotBillService.getInDepotBillById(id);
        List<InDepotBillProduct> list=  inDepotBillProductService.list(new QueryWrapper<InDepotBillProduct>().eq("in_depot_id",id));
        DepotExcalService.InDepotBillProductExcalDownload(idbv.getInputDepotName(),idbv.getBillSn(),list,"库存入库单导出",response);
    }

    @ApiOperation(value = "入库单详情（根据仓库id和盘点时间）")
    @GetMapping("getInDepotBillDetilList")
    public Result getInDepotBillDetilList(DepotBillDetilQuery depotBillDetilQuery){
        return ResultUtil.success(inDepotBillService.getInDepotBillDetilList(initMpPage(),depotBillDetilQuery));
    }

    @ApiOperation(value = "保存入库单及关联商品信息")
    @PostMapping("save")
    @NoRepeatSubmit
    public Result save(@SysInfo SysAuth auth, @RequestBody InDepotBillDto inDepotBillDto){
      Long id=  inDepotBillService.save(inDepotBillDto,auth.getUid());
        return ResultUtil.success(id);
    }
    @ApiOperation(value = "保存入库单及关联商品信息")
    @PostMapping("saveAndungrad")
    @NoRepeatSubmit
    public Result saveAndungrad(@SysInfo SysAuth auth, @RequestBody InDepotBillDto inDepotBillDto){
        Long id=  inDepotBillService.save(inDepotBillDto,auth.getUid());
        return ResultUtil.success(id);
    }
    @ApiOperation(value = "入库审核")
    @PostMapping("audit")
    @NoRepeatSubmit
    public Result audit(@SysInfo SysAuth auth, @RequestBody InDepotBillDto inDepotBillDto){
        Long id=  inDepotBillService.saveAndAudit(inDepotBillDto,auth.getUid());
        return ResultUtil.success(id);
    }
  /*  @ApiOperation(value = "修改是否入库状态（先调用库存入库方法 才能用该方法修改状态）")
    @PostMapping("inDepotChgIsFinish")
    public Result inDepotChgIsFinish(InDepotChgIsFinishDto inDepotChgIsFinishDto){
        inDepotBillService.inDepotChgIsFinish(inDepotChgIsFinishDto);
        return ResultUtil.success();
    }
*/
  /*
  * 删除入库单的同时，入库的货品也需要删除
  *
  * */
    @ApiOperation(value = "删除入库单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "arr",value = "入库单id数组",paramType = "path",dataType = "long")
    })
    @PostMapping("delAll/{id}")
    public Result delAll(@PathVariable Long id){
        InDepotBill bill=inDepotBillService.getById(id);
        if(bill.getIsFinish().equals(WhetherEnum.YES.getValue())){
            return ResultUtil.error("无法删除已经入库的单据");
        }
        inDepotBillService.removeById(id);
        inDepotBillProductService.remove(new QueryWrapper<InDepotBillProduct>().eq("in_depot_id",id));
        return ResultUtil.success();
    }

    @ApiOperation("入库单批量导入数据处理")
    @PostMapping(value = "importExcal")
    public Result productStockImportData(MultipartFile file){

        InDepotBillDataListener idbdl = new InDepotBillDataListener(itemFeign);
        try {
            EasyExcel.read(file.getInputStream(), ProductStockED.class,idbdl).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error("failed");
        }
        return ResultUtil.success(idbdl.getReturnList());
    }

    public static void main(String[] args) {

        System.out.println( DateUtil.currentSeconds());
        System.out.println( DateUtil.currentSeconds());
    }

}
